package com.swf.jpa.util;

import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;

import com.swf.jpa.framework.constant.MockConstants;

public class JPAUtil {
	private static final Log logger = LogFactory.getLog(JPAUtil.class);
	public static <T>  String bean2Json(T bean) throws IOException{
		StringWriter sw = new StringWriter();
		ObjectMapper objectMapper = new ObjectMapper();
		JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(sw);
		jsonGenerator.writeObject(bean);
		jsonGenerator.flush();
		jsonGenerator.close();
		return sw.toString();
		
	}
	public static String Object2Json(Object o) throws IOException{
		ObjectMapper objectMapper = new ObjectMapper();
		return objectMapper.writeValueAsString(o);
	}
	public static String toString(Object c) {
		String result = null;
			try{
			StringBuilder sb = new StringBuilder();
			Field[] fields = c.getClass().getDeclaredFields();
			Object value = "";
			for (Field f:fields){
				f.setAccessible(true);
				if (isSimpleType(f.getType())){
					value = f.get(c);
					if (value!=null){
						sb.append(f.getName()+"="+value+"&");
					}
				}
			}
			result = sb.toString();
			if (result.endsWith("&")){
				result = c.getClass().getSimpleName()+"["+result.substring(0, result.length()-1)+"]";
			}
			}catch(IllegalAccessException e){
				e.printStackTrace();
			}
		return result;
	}
	
	public static String toHTMLPropertiesStr(Object c,String fileSeparator){
		Map<String,Object> map = toMap(c);
		StringBuffer sb = new StringBuffer();
		for (String key:map.keySet()){
			sb.append(key+"="+map.get(key)+fileSeparator);
		}
		return sb.toString();
	}
	
	public static String toJson(Object c){
		Map<String,Object> map = toMap(c);
		try {
			return bean2Json(map);
		} catch (IOException e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}
	
	public static Map<String,Object> toMap(Object c) {
		Map<String,Object> map = new LinkedHashMap<String,Object>();
			Field[] fields = c.getClass().getDeclaredFields();
			Object value = "";
			for (Field f:fields){
				f.setAccessible(true);
				if (isSimpleType(f.getType())){
					try {
						value = f.get(c);
					} catch (IllegalArgumentException e) {
						logger.error(e.getMessage(), e);
					} catch (IllegalAccessException e) {
						logger.error(e.getMessage(), e);
					}
					if (value!=null){
						map.put(f.getName(),value);
					}
				}
			}
		return map;
	}
	
	public static boolean isSimpleType(Class<?> c){
		return equalsOne(c, new Class[]{
				int.class,double.class,float.class,boolean.class,
				Character.class,Boolean.class,String.class,Integer.class,
				Double.class,Float.class});
	}
	public static boolean equalsOne(Class<?> c,Class<?>[] allClass){
		for (Class<?> current:allClass){
			if ( c == current){
				return true;
			}
		}
		return false;
	}
	
	public static String changeStandard(String value){
		value = value.toLowerCase();
		String[] strs = value.split("_");
		StringBuilder sb = new StringBuilder(strs[0]);
		for (int i=1;i<strs.length;i++){
			sb.append((char)(strs[i].charAt(0)-32)).append(strs[i].substring(1));
		}
		return sb.toString();
	}
	public static void createModel(Class<?> c,boolean needIndex){
		Field[] fields = c.getDeclaredFields();
		for (Field f:fields){
			f.setAccessible(true);
			String fieldName = f.getName();
			String fName = "";
			if (needIndex){
				fName = fieldName.substring(fieldName.indexOf("_")+1).toLowerCase();
			}else{
				fName = changeStandard(fieldName);
			}
			System.out.println("private String "+  fName+";");
		}
		for (Field f:fields){
			f.setAccessible(true);
			String fieldName = f.getName();
			String fName;
			String ufName;
			if (needIndex){
				fName = fieldName.substring(fieldName.indexOf("_")+1).toLowerCase();
			}else{
				fName = changeStandard(fieldName);
			}
			ufName = fName.substring(0,1).toUpperCase()+fName.substring(1).toLowerCase();
			System.out.println("@Column(name=\""+fieldName+"\")");
			System.out.println("public String get"+ufName+"(){return "+fName+";}");
			System.out.println("public String set"+ufName+"(String "+fName+"){return this."+fName +"=" +fName+";}");
		}
	}
	
}
